﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace QiangPiao.Person
{
    public class ConfigBase
    {
        private const string ALARMFILE = "PersonList.config";

        public static string GetXmlPath()
        {
            string str = AppDomain.CurrentDomain.BaseDirectory + "\\";

            return str + ALARMFILE;
        }

        public static List<Persons> GetPerson()
        {
            object obj = new object();
            string configPath = GetXmlPath();
            if (File.Exists(configPath))
            {
                using (Stream stream = new FileStream(configPath, FileMode.Open, FileAccess.Read))
                {
                    obj = new XmlSerializer(typeof(List<Persons>)).Deserialize(stream);
                }
            }
            return obj as List<Persons>;
        }

        public static void UpdatePerson(List<Persons> list)
        {
            if (list != null)
            {
                string configPath = GetXmlPath();
                try
                {
                    XmlSerializer serializer = new XmlSerializer(list.GetType());
                    using (Stream stream = new FileStream(configPath, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
                        namespaces.Add("", "");
                        serializer.Serialize(stream, list, namespaces);
                    }
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
        }
    }
}
